<?php
namespace inkstones\session;
use inkstones\config\Config;
use inkstones\session\RedisSession;

class Session{
	
	protected static $active=false;
	
	public static function config(array $config=array()){
		
		$config=empty($config)?Config::get('session'):$config;
		
		$active=false;
		
		isset($config['session.use_trans_sid'])?ini_set('session.use_trans_sid',$config['session.use_trans_sid']?1:0):'';//当客户端禁用cookie时自动将sessionid附在URL中来实现会话
		
		!empty($config['auto_start']) && PHP_SESSION_ACTIVE !=session_status()?ini_set('session.auto_start',0) && $active=true:'';
		
		//session名称
		if(isset($config['sname'])){
			session_name($config['sname']);
		}
		
		//域名
		if (isset($config['domain'])) {
            ini_set('session.cookie_domain', $config['domain']);
        }
		
		//有效时间
		if (isset($config['expire'])) {
            ini_set('session.gc_maxlifetime', $config['expire']);
            ini_set('session.cookie_lifetime', $config['expire']);
        }
		
		//使用HTTPS发送cookie
        if (isset($config['secure'])) {
            ini_set('session.cookie_secure', $config['secure']);
        }
		
		//一次性cookies
        if (isset($config['httponly'])) {
            ini_set('session.cookie_httponly', $config['httponly']);
        }
		
		if (isset($config['use_session_id']) && isset($_REQUEST[$config['use_session_id']])) {
            session_id($_REQUEST[$config['use_session_id']]);
        } elseif (isset($config['id']) && !empty($config['id'])) {
            session_id($config['id']);
        }
		
		//设置客户端是否使用cookie来保存sessionid
        if (isset($config['use_cookies'])) {
            ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
        }
		
		//缓存类型
        if (isset($config['cache_limiter'])) {
            session_cache_limiter($config['cache_limiter']);
        }
		
        if (isset($config['cache_expire'])) {
            session_cache_expire($config['cache_expire']);
        }
		
		if (isset($config['path'])) {
            session_save_path($config['path']);
        }
		
		if(!empty($config['stype'])){
			$session=strpos($config['stype'],'\\')?$config['stype']:'\\inkstones\\session\\'.ucwords($config['stype']);
		}
		
		if(class_exists($session)){
			session_set_save_handler(new $session($config)) or die('session handler error!');
		}
		else{
			die('class not exists '.$config['stype']);
		}
		
		if($active){
			session_start();
			self::$active=true;
		}
		else{
			self::$active=false;
		}
		
	}
	
	public static function set($key,$val=''){
		self::$active && self::auto_start();
		if(strpos($key,'.')){
			list($key1,$key2)=explode('.',$key);
			$_SESSION[$key1][$key2]=$val;
		}
		else{
			$_SESSION[$key]=$val;
		}
	}
	
	public static function get($key=''){
		self::$active && self::auto_start();
		$session='';
		if(empty($key)){
			$session=$_SESSION;
		}
		elseif(strpos($key,'.')){
			list($key1,$key2)=explode('.',$key);
			$session=isset($_SESSION[$key1][$key2])?$_SESSION[$key1][$key2]:null;
		}
		else{
			$session=isset($_SESSION[$key])?$_SESSION[$key]:null;
		}
		return $session;
	}
	
	public static function auto_start(){
		if(self::$active===false){
			if(session_status != PHP_SESSION_ACTIVE){
				session_start();
			}
			self::$active=true;
		}
	}
	
	public static function reset_id($flag=false){
		session_regenerate_id($flag);
	}
	
	public static function del($key){
		self::$active && self::auto_start();
		if(strpos($key,'.')){
			list($k1,$k2)=explode('.',$key);
			unset($_SESSION[$k1][$k2]);
		}
		elseif(is_array($key)){
			foreach($key as $k => $v){
				self::del($v);
			}
		}
		else{
			unset($_SESSION[$key]);
		}
	}
	
	public static function stop(){
		session_write_close();
		self::$active=false;
	}
	
	public static function start(){
		session_start();
		self::$active=true;
	}
	
	public static function clear(){
		self::$active && self::auto_start();
		$_SESSION=array();
	}
	
	public static function has($key){
		self::$active && self::auto_start();
		if(strpos($key,'.')){
			list($k1,$k2)=explode('.',$key);
			return isset($_SESSION[$k1][$k2]);
		}
		else{
			return isset($_SESSION[$key]);
		}
	}
	
	public function test(){
		echo 'test';
		$c=new Config();
		$c->t();
	}
	
}















?>





















